package com.qlylkj.lbk.ui.user.password

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.core.widget.addTextChangedListener
import com.forest.core.etx.isPassword
import com.forest.core.etx.isPhoneNumber
import com.forest.core.etx.isVerify
import com.forest.core.etx.toast
import com.forest.core.widget.SmsCountDownTimer
import com.qlylkj.lbk.R
import com.qlylkj.lbk.base.BaseActivity
import kotlinx.android.synthetic.main.activity_find_password.*
import kotlinx.android.synthetic.main.layout_toolbar.*

class FindPasswordActivity: BaseActivity() {

    private var passwordShow = false

    private var smsCountDownTimer: SmsCountDownTimer? = null

    override fun setLayoutId() = R.layout.activity_find_password

    override fun initView() {
        toolbar.setNavigationOnClickListener { finish() }
        toolbarTitle.text = getString(R.string.activity_find_password)
        getVerifyCode.isSelected = true
        phoneNumberEdit.addTextChangedListener { check() }
        verifyCodeEdit.addTextChangedListener { check() }
        passwordEdit.addTextChangedListener { check() }
        getVerifyCode.setOnClickListener(onClickListener)
        passwordStatusImage.setOnClickListener(onClickListener)
    }

    override fun initData() {

    }

    private val onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.getVerifyCode -> getVerifyCode()
            R.id.passwordStatusImage -> changePasswordStatus()
            R.id.findPassword -> findPassword()
        }
    }

    private fun check() {
        val check = phoneNumberEdit.isPhoneNumber() && verifyCodeEdit.isVerify() && passwordEdit.isPassword(8)
        findPassword.isEnabled = check
        findPassword.isSelected = check
    }

    private fun changePasswordStatus() {
        if (passwordShow) {
            passwordEdit.transformationMethod = PasswordTransformationMethod.getInstance()
            passwordStatusImage.setImageResource(R.mipmap.ic_password_display)
        } else {
            passwordEdit.transformationMethod = HideReturnsTransformationMethod.getInstance()
            passwordStatusImage.setImageResource(R.mipmap.ic_password_show)
        }
        passwordShow = !passwordShow
    }

    private fun getVerifyCode(){
        smsCountDownTimer = SmsCountDownTimer(getVerifyCode, "获取验证码")
        smsCountDownTimer?.start()
        toast("短信验证码已发送")
    }

    private fun findPassword(){

    }

    override fun onDestroy() {
        smsCountDownTimer?.cancel()
        super.onDestroy()
    }

}